C language int main of int argc char *argv[]

  • 2020-05-12 03:00:54
  • OfStack

int main(int argc,char *argv[])

argc is the total number of arguments on the command line;

argv[] is the argc parameter, of which the 0th parameter is the full name of the program and the subsequent parameter. The user-entered parameters that follow the command line.


int  main(int  argc,  char*  argv[]) 
  { 
  int  i; 
  for  (i  =  0;  i<argc;  i++) 
  cout<<argv[i]<<endl; 
  cin>>i; 
  return  0; 
  } 

Type in during execution


 F:\MYDOCU~1\TEMPCODE\D1\DEBUG\D1.EXE  aaaa  bbb  ccc  ddd 

The output is as follows:


 F:\MYDOCU~1\TEMPCODE\D1\DEBUG\D1.EXE 
  aaaa 
  bbb 
  ccc 
  ddd 

--------------------------------------------------------------------

char *argv[] is an array of 1 character, the size is int argc, mainly used for the command line parameter argv[], each element in the array represents 1 parameter;

Let's say you type in


 test  a.c  b.c  t.c 
   the  
  argc  =  4 
  
  argv[0]  =  "test" 
  argv[1]  =  "a.c" 
  argv[2]  =  "b.c" 
  argv[3]  =  "t.c"

--------------------------------------------------------------------------------------------

argc records the number of arguments the user enters on the command line of the program.

The array that arg[] points to has at least one character pointer, arg[0]. It usually points to the file name of the executable in the program. Programs are also included in some versions of the compiler
The path where the file is located.


int main(int argc, char ** argv)
{
 int i;
  for (i=0; i < argc; i++)
    printf("Argument %d is %s.\n", i, argv[i]);

  return 0;
}

The above main functions are often used in OpenCV programming,

Where argc stands for: number of parameters input

Where argv stands for: the storage path of this executable file, program variables

For example, you create a project under D disk and name it cvshow. At the same time, you create an cpp source file named cvshow under Debug. When the executable program generates show.exe executable file under Debug, Debug is located under cvshow, so the storage path of show.exe is

D:\cvshow\Debug\ show. exe, the value of argv[0]

argv[1], argv[2], argv[3], once represent input variables separated by Spaces

Example:

The input variable is: c:\ baboon.jpg

So, argc==1 argv[0]==D:\cvshow\Debug\ show. exe argv[1]==c:\ baboon. jpg

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: